<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\BatchChangeClueTagForm;
use App\Admin\Widgets\SuperModal as WidgetModal;
use App\Models\Clue;
use App\Models\Tag;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class TagController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Tag(), function (Grid $grid) {
            $grid->model()
                ->withCount(['clues' => function (Builder $query) {
                    $query->whereBetween('post_date', [now()->startOfMonth(), now()->endOfMonth()]);
                }])
                ->orderBy('order');
            $grid->sortable();
            $grid->header(function () {
                $count = Clue::query()->select(['tag_id'])->whereNull('tag_id')->count();
//                $data = Clue::select(
//                    DB::raw('count(id) as `data`'),
//                    DB::raw("DATE_FORMAT(post_date, '%Y-%m') as new_date")
//                )
//                    ->whereNotNull('post_date')
//                    ->groupBy('new_date')->orderBy('new_date')->get()->pluck('data','new_date');
                if (!$count) return '';
                $bUrl = admin_route('admin.clue.user', [
                    '_tag_id' => '1',
                ]);

                $modal = WidgetModal::make()
                    ->title("批量修改标签")
                    ->lg()
                    ->body(new BatchChangeClueTagForm())
                    ->delay(300)
                    ->button("<a href='javascript:void(0)' style='color:inherit;'>(批量修改)</a>");
                return new Alert("未添加标签线索数量: <a href='$bUrl' style='color:inherit;'>$count</a> $modal");
            });

//            $grid->column('order','排序');
            $grid->column('name');
            $grid->column('clues_count', '本月使用次数');

            $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                $create->text('name', '名称');
            });
            $grid->disableCreateButton();
            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('name');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Tag(), function (Show $show) {
            $show->field('id');
            $show->field('name');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Tag(), function (Form $form) {
            $form->display('id');
            $form->text('name');
        });
    }
}
